package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.Position;
import com.sdry.service.hpf.PositionService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: PositionController 
 * @Description:工位信息Controller
 * @Author hpf
 * @Date 2018年9月21日 下午2:52:46 
 * @version 1.0
 */
@RequestMapping(value="/positionMana")
@Controller
public class PositionController {

	@Resource
	private PositionService positionService;
	
	/**
	 * 分页查询工位信息
	 * @param page 分页条件
	 * @param position 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPagePosition")
	public void listPagePosition(Page page,Position position,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (position.getPname() != null) {
			map.put("pname", position.getPname());
		} else {
			position.setPname("");
			map.put("pname", "");
		}
		int count = positionService.countPosition(position);
		List<Position> positionList = positionService.listPagePosition(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(positionList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存工位信息
	 * @param position
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/savePosition")
	public void savePosition(Position position,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (position.getPid() ==null || "".equals(position.getPid())) {
			isSuccess = positionService.insertPosition(position);
		} else {
			isSuccess = positionService.updatePosition(position);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除工位信息
	 * @param pid 工位ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deletePosition")
	public void deletePosition(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = positionService.deletePosition(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除工位
	 * @param str 工位ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deletePositionOfVerify")
	public void deletePositionOfVerify(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = positionService.deletePositionOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验工位名称是否重复
	 * @param pname 工位名称
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/verifyPositionName")
	public void verifyPositionName(String pname,HttpServletResponse response) throws Exception{
		int isSuccess = positionService.verifyPositionName(pname);
		ResponseUtil.write(response, isSuccess);
	}
}
